<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户类型：1-管理员用户，2-会员用户" prop="userType">
        <el-select
          v-model="queryParams.userType"
          placeholder="请选择用户类型：1-管理员用户，2-会员用户"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="活跃日期" prop="activityDate">
        <el-date-picker
          v-model="queryParams.activityDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="是否活跃：0-否，1-是" prop="isActive">
        <el-select
          v-model="queryParams.isActive"
          placeholder="请选择是否活跃：0-否，1-是"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否新用户：0-否，1-是" prop="isNewUser">
        <el-select
          v-model="queryParams.isNewUser"
          placeholder="请选择是否新用户：0-否，1-是"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="登录次数" prop="loginCount">
        <el-input
          v-model="queryParams.loginCount"
          placeholder="请输入登录次数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="首次登录时间" prop="firstLoginTime">
        <el-date-picker
          v-model="queryParams.firstLoginTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="最后登录时间" prop="lastLoginTime">
        <el-date-picker
          v-model="queryParams.lastLoginTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="在线时长（分钟）" prop="onlineDuration">
        <el-input
          v-model="queryParams.onlineDuration"
          placeholder="请输入在线时长（分钟）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="页面浏览次数" prop="pageViews">
        <el-input
          v-model="queryParams.pageViews"
          placeholder="请输入页面浏览次数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="API调用次数" prop="apiCalls">
        <el-input
          v-model="queryParams.apiCalls"
          placeholder="请输入API调用次数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="功能使用次数" prop="featureUsageCount">
        <el-input
          v-model="queryParams.featureUsageCount"
          placeholder="请输入功能使用次数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备类型：PC/Mobile/Tablet" prop="deviceType">
        <el-select
          v-model="queryParams.deviceType"
          placeholder="请选择设备类型：PC/Mobile/Tablet"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="浏览器类型" prop="browserType">
        <el-select
          v-model="queryParams.browserType"
          placeholder="请选择浏览器类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作系统类型" prop="osType">
        <el-select
          v-model="queryParams.osType"
          placeholder="请选择操作系统类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="IP地址" prop="ipAddress">
        <el-input
          v-model="queryParams.ipAddress"
          placeholder="请输入IP地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="地域信息" prop="region">
        <el-input
          v-model="queryParams.region"
          placeholder="请输入地域信息"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="城市信息" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入城市信息"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="连续活跃天数" prop="consecutiveDays">
        <el-input
          v-model="queryParams.consecutiveDays"
          placeholder="请输入连续活跃天数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="总活跃天数" prop="totalActiveDays">
        <el-input
          v-model="queryParams.totalActiveDays"
          placeholder="请输入总活跃天数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:user-daily-activity:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:user-daily-activity:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="用户类型：1-管理员用户，2-会员用户" align="center" prop="userType" />
      <el-table-column label="活跃日期" align="center" prop="activityDate" />
      <el-table-column label="是否活跃：0-否，1-是" align="center" prop="isActive" />
      <el-table-column label="是否新用户：0-否，1-是" align="center" prop="isNewUser" />
      <el-table-column label="登录次数" align="center" prop="loginCount" />
      <el-table-column
        label="首次登录时间"
        align="center"
        prop="firstLoginTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="最后登录时间"
        align="center"
        prop="lastLoginTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="在线时长（分钟）" align="center" prop="onlineDuration" />
      <el-table-column label="页面浏览次数" align="center" prop="pageViews" />
      <el-table-column label="API调用次数" align="center" prop="apiCalls" />
      <el-table-column label="功能使用次数" align="center" prop="featureUsageCount" />
      <el-table-column label="设备类型：PC/Mobile/Tablet" align="center" prop="deviceType" />
      <el-table-column label="浏览器类型" align="center" prop="browserType" />
      <el-table-column label="操作系统类型" align="center" prop="osType" />
      <el-table-column label="IP地址" align="center" prop="ipAddress" />
      <el-table-column label="地域信息" align="center" prop="region" />
      <el-table-column label="城市信息" align="center" prop="city" />
      <el-table-column label="连续活跃天数" align="center" prop="consecutiveDays" />
      <el-table-column label="总活跃天数" align="center" prop="totalActiveDays" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:user-daily-activity:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:user-daily-activity:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <UserDailyActivityForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { UserDailyActivityApi, UserDailyActivityVO } from '@/api/system/userdailyactivity'
import UserDailyActivityForm from './UserDailyActivityForm.vue'

/** 用户每日活跃明细 列表 */
defineOptions({ name: 'UserDailyActivity' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<UserDailyActivityVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  username: undefined,
  userType: undefined,
  activityDate: [],
  isActive: undefined,
  isNewUser: undefined,
  loginCount: undefined,
  firstLoginTime: [],
  lastLoginTime: [],
  onlineDuration: undefined,
  pageViews: undefined,
  apiCalls: undefined,
  featureUsageCount: undefined,
  deviceType: undefined,
  browserType: undefined,
  osType: undefined,
  ipAddress: undefined,
  region: undefined,
  city: undefined,
  consecutiveDays: undefined,
  totalActiveDays: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserDailyActivityApi.getUserDailyActivityPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UserDailyActivityApi.deleteUserDailyActivity(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UserDailyActivityApi.exportUserDailyActivity(queryParams)
    download.excel(data, '用户每日活跃明细.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>